package com.yuanxiao.controller;

import com.yuanxiao.api.Service.IpService;
import com.yuanxiao.api.Service.MonitorService;
import com.yuanxiao.utils.NetworkUtil;


import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@RestController
public class controller {

    private static ThreadPoolExecutor poolExecutor =
            new ThreadPoolExecutor(3,
                    3,
                    5,
                    TimeUnit.SECONDS,
                    new ArrayBlockingQueue<Runnable>(10000),
                    new ThreadPoolExecutor.DiscardPolicy());
    @Reference
    private IpService ipService;
    @Reference
    private MonitorService monitorService;

    @RequestMapping("/test")
    @ResponseBody
    public Object test(HttpServletRequest httpServletRequest) throws IOException {
        String ipAddress = NetworkUtil.getIpAddress(httpServletRequest);
        ipService.service(ipAddress);//这里IP是为了和拦截器中获取的ip比较,看是否相同
        return "success";
    }

    @RequestMapping("/test2")

    @ResponseBody
    public Object test2(HttpServletRequest httpServletRequest) throws IOException {
        //monitorService.methodA("A");
        Runnable runnableA =new Runnable() {
            @Override
            public void run() {
                while (true){
                    String methodA = monitorService.methodA("A");
                    System.out.println(methodA);
                    String methodB = monitorService.methodB("B");
                    System.out.println(methodB);
                    String methodC = monitorService.methodC("C");
                    System.out.println(methodC);
                }
            }
        };


        poolExecutor.execute(runnableA);

        return "succes";

    }
}
